import threading
import tkinter
import tkinter.messagebox
import customtkinter

from pynput.keyboard import Key, Listener, Controller

import pyautogui
import time

import random

import sys
import os

customtkinter.set_appearance_mode("Dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()


        self.stop_flag = True

        # configure window
        self.title("连点器")
        self.geometry(f"{300}x{350}")

        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=1)

        self.protocol("WM_DELETE_WINDOW", self.quit)

        self.logo_label = customtkinter.CTkLabel(self, text="连点器", font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))
        self.sidebar_button_1 = customtkinter.CTkButton(self, command=self.sidebar_button_event, text="Start")
        self.sidebar_button_1.grid(row=1, column=0, padx=20, pady=10)
        self.sidebar_button_2 = customtkinter.CTkButton(self, command=self.stopClick, text="Stop")
        self.sidebar_button_2.grid(row=2, column=0, padx=20, pady=10)
        # self.sidebar_button_3 = customtkinter.CTkButton(self, command=self.quit, text="Quit")
        # self.sidebar_button_3.grid(row=3, column=0, padx=20, pady=10)

        self.appearance_mode_label = customtkinter.CTkLabel(self, text="按键", anchor="w")
        self.appearance_mode_label.grid(row=3, column=0, padx=20, pady=(10, 0))
        selets = ["t", "p", "n", ";", ",", ".", "/", "[", "]", "0"]
        self.sidebar_comboBox_1 = customtkinter.CTkComboBox(self, values=selets)
        self.sidebar_comboBox_1.set("t")
        self.sidebar_comboBox_1.grid(row=4, column=0, padx=20, pady=10)

        self.appearance_mode_label = customtkinter.CTkLabel(self, text="主题", anchor="w")
        self.appearance_mode_label.grid(row=5, column=0, padx=20, pady=(10, 0))
        self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self, values=["Light", "Dark", "System"],
                                                                       command=self.change_appearance_mode_event)
        self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10))


        self.listener_thread = threading.Thread(target=self.start_listenerChange)
        self.listener_thread.start()



    def change_appearance_mode_event(self, new_appearance_mode: str):
        customtkinter.set_appearance_mode(new_appearance_mode)


    def sidebar_button_event(self):
        self.stop_flag = False
        self.whileThread = self.startWhile()

    def stopClick(self):
        self.stop_flag = True
        print(self.stop_flag)
        

    def showInfoBox(self):
        tkinter.messagebox.showinfo("Info", "This is a message box")

    def doAWhile(self):
        while not self.stop_flag:
            t = random.randint(0, 5)/10.0
            print(t)
            time.sleep(t)
            print("doing something")
            keyboard = Controller()
            keyboard.press('f')
            keyboard.release('f')
            pyautogui.click()  # 单击


    def startWhile(self):
        thread = threading.Thread(target=self.doAWhile)
        thread.daemon = True
        thread.start()
        return thread



    def on_press(self, key):
        try:
            if key.char == self.sidebar_comboBox_1.get() or key.char == 't':
                self.stop_flag = not self.stop_flag
                print(self.stop_flag)
                self.startWhile()
                    
        except AttributeError:
            pass


    def start_listenerChange(self):
        with Listener(on_press=self.on_press) as listener:
            listener.join()



    def quit(self):
        self.destroy()
        self.stop_flag = True
        os._exit(0)
        



if __name__ == "__main__":
    app = App()
    app.mainloop()
    